package test6;

import java.util.ArrayList;
import java.util.Scanner;

public class begain {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        User u1 = new User("001", "张三", "123456");
        User u2 = new User("002", "李四", "1234567");
        User u3 = new User("003", "王五", "12345646");

        list.add(u1);
        list.add(u2);
        list.add(u3);
        System.out.println("请输入您要查找的用户id:");
        while (true) {
            Scanner sc = new Scanner(System.in);
            String id = sc.next();
            int result = contains(list, id);
            if (result >= 0) {
                System.out.println("查找用户存在,他的姓名为:");
                User u = list.get(result);
                String name = u.getUsername();
                System.out.println(name);

            }
            else {
                System.out.println("输入id不存在,请重新输入:");
            }


        }
    }

    public static int contains(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            String uid = u.getId();
            if (uid.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
